﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AdvancedDataGridView
{
	public class TreeGridNodeCollection : System.Collections.Generic.IList<TreeGridNode>, System.Collections.IList
	{
		internal System.Collections.Generic.List<TreeGridNode> _list;
		internal TreeGridNode _owner;
		internal TreeGridNodeCollection(TreeGridNode owner)
		{
			this._owner = owner;
			this._list = new List<TreeGridNode>();
		}

		#region Interface Implementation Members

		public int IndexOf(TreeGridNode item)
		{
			return this._list.IndexOf(item);
		}
		int System.Collections.IList.IndexOf(object item)
		{
			return this.IndexOf(item as TreeGridNode);
		}

		public void Insert(int index, TreeGridNode item)
		{
			// The row needs to exist in the child collection before the parent is notified.
			item._grid = this._owner._grid;
			item._owner = this;

			this._list.Insert(index, item);

			this._owner.InsertChildNode(index, item);
		}
		void System.Collections.IList.Insert(int index, object value)
		{
			this.Insert(index, value as TreeGridNode);
		}

		public void Add(TreeGridNode item)
		{
			// The row needs to exist in the child collection before the parent is notified.
			item._grid = this._owner._grid;
			item._owner = this;

			this._list.Add(item);

			this._owner.AddChildNode(item);
		}
		int System.Collections.IList.Add(object value)
		{
			TreeGridNode item = value as TreeGridNode;
			this.Add(item);
			return item.Index;
		}

		public bool Remove(TreeGridNode item)
		{
			// The parent is notified first then the row is removed from the child collection.
			this._owner.RemoveChildNode(item);
			item._grid = null;
			return this._list.Remove(item);
		}
		void System.Collections.IList.Remove(object value)
		{
			this.Remove(value as TreeGridNode);
		}

		public void RemoveAt(int index)
		{
			TreeGridNode row = this._list[index];

			// The parent is notified first then the row is removed from the child collection.
			this._owner.RemoveChildNode(row);
			row._grid = null;
			this._list.RemoveAt(index);
		}
		void System.Collections.IList.RemoveAt(int index)
		{
			this.RemoveAt(index);
		}

		public void Clear()
		{
			// The parent is notified first then the row is removed from the child collection.
			this._owner.Clear();
			this._list.Clear();
		}
		void System.Collections.IList.Clear()
		{
			this.Clear();
		}

		public TreeGridNode this[int index]
		{
			get
			{
				return this._list[index];
			}
			set
			{
				throw new Exception(Resources.MethodNotImplemented);
			}
		}
		object System.Collections.IList.this[int index]
		{
			get
			{
				return this[index];
			}
			set
			{
				throw new Exception(Resources.MethodNotImplemented);
			}
		}

		public bool Contains(TreeGridNode item)
		{
			return this._list.Contains(item);
		}
		bool System.Collections.IList.Contains(object value)
		{
			return this.Contains(value as TreeGridNode);
		}

		public void CopyTo(TreeGridNode[] array, int arrayIndex)
		{
			throw new Exception(Resources.MethodNotImplemented);
		}
		void System.Collections.ICollection.CopyTo(Array array, int index)
		{
			throw new Exception(Resources.MethodNotImplemented);
		}


		public int Count
		{
			get
			{
				return this._list.Count;
			}
		}
		int System.Collections.ICollection.Count
		{
			get { return this.Count; }
		}

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}
		bool System.Collections.IList.IsReadOnly
		{
			get { return this.IsReadOnly;}
		}

		bool System.Collections.IList.IsFixedSize
		{
			get { return false; }
		}


		#region IEnumerable<ExpandableRow> Members

		public IEnumerator<TreeGridNode> GetEnumerator()
		{
			return this._list.GetEnumerator();
		}

		#endregion


		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}

		#endregion
		#endregion


		#region ICollection Members

		bool System.Collections.ICollection.IsSynchronized
		{
			get { throw new Exception(Resources.MethodNotImplemented); }
		}

		object System.Collections.ICollection.SyncRoot
		{
			get { throw new Exception(Resources.MethodNotImplemented); }
		}

		#endregion
	}

}
